/*!
 * 面板控件.
 * ecp.qzz.ui.design.table.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.dbase"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.design");
	/**
	 * 面板控件.
	 */
	qzz.ui.design.table = qzz.Class(qzz.ui.design.base, {
		/**
		 * 创建函数
		 */
	    _create:function() {	    	
	    	this.base();  
	    	this.className = "Table";
            this.getOption().rows = this._getNumberValue(this.getOption().rows, 1);
            this.getOption().cols = this._getNumberValue(this.getOption().cols, 1);
            this.getOption().heads = this._getNumberValue(this.getOption().heads, 1);
            this._rowHeight = this._getNumberValue(this.getOption().rowHeight, 24);
            if(this.getOption().height == null || this.getOption().height === "" || this.getOption().height <= 10) {
                this.getOption().height = this._rowHeight * this.getOption().rows;
            }
            this._headRows = 1;
            this._fixCols = 0;
            this._table = null;
            this._cellMouseDown = false;
            this._selRect = {"brow":1,"erow":1,"bcol":1,"ecol":1};
            this._sheet = [];
            this._selectType = "td";
	    },
	    /**
	     * 渲染.
	     */
	    _render:function() {
	    	this.base();
	    	this._table = this.createElement("TABLE");
	    	this._table.onselectstart = this._onselectstart;
	    	this.setStyle(this._table, "noborder");
	    	this._reset();
	    	this._table.style.width = "100%";
	    	this._table.style.height = "100%";
	    	this._table.style.tableLayout = "fixed";
	    	if(this.getOption().border != null && this.getOption().border !== "") {
	    		this.thisWindow.style.borderWidth = this.getOption().border + "px ";
	    	} else {
	    	    this.thisWindow.style.borderWidth = "1px"
	    	}
	    	this.thisWindow.style.borderStyle = this.getOption().borderStyle || "solid";
	    	this._setHtml(this.thisWindow, "", false);
	    	this.thisWindow.appendChild(this._table);
	    	
	    	this._tool = this.createElement("TABLE");
	    	this.setStyle(this._tool, "print_table_tool");
	    	
	    	var that = this;
	    	this._tool_merg = this.createElement("DIV");
	    	this._tool_merg.title = this.getI18n("qzz.merg", "合并/拆分单元格");
	    	this.setStyle(this._tool_merg, "print_table_tool_merg");
	    	var tr = this._tool.insertRow(0);
	    	var k = 0;
	    	var td = tr.insertCell(k++);
	    	td.style.padding = "1px";
	    	td.appendChild(this._tool_merg);	    	
	    	this._tool_merg.onclick = function(ev) {
	    		that._mergCell();
	    	}
//	    	this._tool_reset = this.createElement("DIV");
//	    	this._tool_reset.title = "重置表格";
//	    	this.setStyle(this._tool_reset,"print_table_tool_reset");
//	    	td = tr.insertCell(k++);
//	    	td.style.padding = "1px";
//	    	td.appendChild(this._tool_reset);
//	    	this._tool_reset.onclick = function(ev) {
//	    		that._reset();
//	    	}	    	
	    	//this._tool.style.width = "";
	    	this.thisWindow.appendChild(this._tool);	    	
	    },
	    /**
	     * 列变更函数.
	     */
	    _changeCol:function() {	    	
	    	var oCols = this._table.rows[0].cells.length - 1;
	    	var nCols = this.getOption().cols;
	    	if(nCols < oCols) {
	    		//this.getOption().cols = oCols;
		    	for(var i = this.getOption().rows; i >= 0; i--) {
		    		var tr = this._table.rows[i];
		    		//修改所有夸列过长的列
		    		for(var j = oCols; j > nCols; j--) {
		    			var td = null;
		    			if(i > 0) {
		    			    td = this._sheet[i][j];
		    			} else {
		    				td = this._table.rows[0].cells[j];
		    			}
		    			if(i == 0) {
		    				tr.deleteCell(j);
		    			} else if(td != null && td.parentNode.rowIndex == i) {
		    				if(td.colSpan == null || td.colSpan == 1) {
		    					var cellIndex = td.cellIndex;
		    					tr.deleteCell(cellIndex);
		    				} else {
		    					td.colSpan = parseInt(td.colSpan, 10) - 1;
		    				}
		    			}
		    			td = null;
		    		}
		    		tr = null;
		    	}
		    	var tr = this._table.rows[0];
		    	var sumw = 1;
		    	for(var j = 1, len = tr.cells.length; j < len; j++) {
		    		sumw += this._getNumberValue(tr.cells[j].style.width, 1);
		    	}
		    	//this.getOption().cols = nCols;
		    	tr = null;
		    	this._oldWidth = sumw;
		    	this.setWidth(parseInt(sumw/this.getOption().scale,10));
	    	} else {
		    	var avgw = 1;
		    	var sumw = 1;
		    	var tr = this._table.rows[0];
		    	for(var i = 1; i <= oCols; i++) {
		    		var td = tr.cells[i];
		    		sumw += this._getNumberValue(td.style.width, 1);
		    		if(i == 1) {
		    		    avgw = this._getNumberValue(td.style.width, 1);
		    		}
		    		td = null;
		    	}
		    	tr = null;
	    		//重新生成
		    	for(var i = 0; i <= this.getOption().rows; i++) {
		    		var tr = this._table.rows[i];
		    		var ic = nCols - oCols;
		    		for(var j = 0; j < ic; j++) {
		    			var mj = tr.cells.length;
		    			var td = tr.insertCell(mj);
		    			if(i == 0) {
		    				td.style.height = "1px";
			    			td.style.width = avgw + "px";
			    			sumw += avgw;
		    			} else {
			    			this._initCellEvent(td);
			    			this._setCellStyle(td);
		    			}	
		    			td = null;
		    		}
		    		tr = null;
		    	}
		    	this._oldWidth = sumw;
		    	this.setWidth(parseInt(sumw/this.getOption().scale,10));
	    	}
	    	this._refreshSheet();	    	
	    },
	    /**
	     * 行变更函数.
	     */
	    _changeRow:function() {
	    	var oRows = this._table.rows.length - 1;
	    	var nRows = this.getOption().rows;
	    	if(this.getOption().heads > nRows) {
	    		this.getOption().heads = nRows;
	    	}
	    	if(nRows < oRows) {
	    		var i = 1;
	    		var sumh = 1;
		    	for(var i = 1; i <= nRows; i++) {
		    		var tr = this._table.rows[i];
		    		var cols = tr.cells.length - 1;
		    		for(var j = 0; j <= cols; j++) {
		    			var td = tr.cells[j];
		    			var rowSpan = td.rowSpan;
		    			if(rowSpan != null && rowSpan > nRows - i + 1) {
		    				td.rowSpan = nRows - i + 1;
		    			}
		    		}
		    		sumh += this._getNumberValue(this._table.rows[i].cells[0].style.height, 1);
		    	}
		    	for(var i = oRows; i > nRows; i--) {
		    	    this._table.deleteRow(i);
		    	}
		    	this._oldHeight = sumh;
		    	this.setHeight(parseInt(sumh/this.getOption().scale,10));
	    	} else {
		    	var sumh = 1;
		    	var avgh = 1;
		    	for(var i = 1; i <= oRows; i++) {
		    		var tr = this._table.rows[i];
		    		var td = tr.cells[0];
		    		sumh += this._getNumberValue(td.style.height, 1);
		    		if(i == 1) {
		    			avgh = this._getNumberValue(td.style.height, 1);
		    		}
		    	}
	    		//重新生成
		    	for(var i = oRows + 1; i <= nRows; i++) {
		    		var tr = this._table.insertRow(i);
		    		for(var j = 0; j <= this.getOption().cols; j++) {
		    			var td = tr.insertCell(j);
		    			if(j == 0) {
		    				td.style.width = "1px";
			    			td.style.height = avgh + "px";
			    			sumh += avgh;
		    			} else {
			    			this._initCellEvent(td);
			    			this._setCellStyle(td);
		    			}		    			
		    		}
		    	}
		    	this._oldHeight = sumh;
		    	this.setHeight(parseInt(sumh/this.getOption().scale, 10));
	    	}
	    	this._refreshSheet();
	    },
	    _reset:function() {
	    	var avgw = parseInt(this.getOption().width * this.getOption().scale/this.getOption().cols, 10);
	    	var avgh = parseInt(this.getOption().height * this.getOption().scale/this.getOption().rows, 10);
	    	var sumw = 1, sumh = 2;
	    	if(this.getOption().border != null && this.getOption().border !== "") {
	    		sumh = parseInt(this.getOption().border, 10) + 2;
	    		sumw = parseInt(this.getOption().border, 10);
	    	}
	    	//清除旧数据
	    	for(var i = this._table.rows.length - 1; i >= 0 ; i--) {
	    		this._table.deleteRow(i);
	    	}
	    	//重新生成
	    	for(var i = 0; i <= this.getOption().rows; i++) {
	    		var tr = this._table.insertRow(i);
	    		for(var j = 0; j <= this.getOption().cols; j++) {
	    			var td = tr.insertCell(j);
	    			if(i == 0) {
	    				td.style.height = "1px";
	    				if(j == 0) {
	    					td.style.width = "1px";
	    				} else {
	    					if(j < this.getOption().cols) {
		    				    td.style.width = avgw + "px";
		    				    sumw += avgw;
	    					} else {
	    						td.style.width = (parseInt(this.getOption().width * this.getOption().scale, 10) - sumw) + "px";
	    					}
	    				}
	    			} else if(j == 0) {
	    				td.style.width = "1px";
	    				if(i < this.getOption().rows) {
		    				td.style.height = avgh + "px";
		    				sumh += avgh;
	    				} else {
	    					td.style.height = (parseInt(this.getOption().height * this.getOption().scale, 10) - sumh) + "px";
	    				}
	    			} else {
		    			this._initCellEvent(td);
		    			this._setCellStyle(td);
	    			}
	    			
	    		}
	    	}
	    	this._refreshSheet();
	    },
	    _setCellStyle:function(td) {
	    	this.setStyle(td, "print_table_cell");
			td.style.borderStyle = this.getOption().borderStyle;
			td.style.borderColor = this.getOption().borderColor;
			if(this.getOption().border != null && this.getOption().border !== "") {
				this._setNumberValue(td, "borderRightWidth", this.getOption().border);
				this._setNumberValue(td, "borderBottomWidth", this.getOption().border);
				this._setNumberValue(td, "borderTopWidth", 0);
				this._setNumberValue(td, "borderLeftWidth", 0);
			}
	    },
	    _mergCell:function() {
	    	//合并
    		var colSpan = this._selRect.ecol - this._selRect.bcol + 1;
    		var rowSpan = this._selRect.erow - this._selRect.brow + 1;
    		var ccell = this._sheet[this._selRect.brow][this._selRect.bcol];
    		if(ccell == null) {
    			return;
    		}
    		var ccsp = this._getNumberValue(ccell.colSpan, 1);
    		var crsp = this._getNumberValue(ccell.rowSpan, 1);
	    	if(crsp == rowSpan && ccsp == colSpan) {
	    		//插入拆分后不存在的单元格
	    		ccell.rowSpan = 1;
	    		ccell.colSpan = 1;	    		
	    		for(var i = this._selRect.brow; i <= this._selRect.erow; i++) {
	    			var rows = this._table.rows[i];
	    			for(var j = this._selRect.bcol; j <= this._selRect.ecol; j++) {	
	    				if(i == this._selRect.brow && j == this._selRect.bcol) {
	    					continue;
	    				}
	    				var cellIndex = 0;
	    				var k = 1;
	    				var cell = this._sheet[i][j - k];	    				
	    				while(cell != null && cell.parentNode.rowIndex != i && j - k > 0) {
	    					k++;
		    				cell = this._sheet[i][j - k];
	    				}
	    				if(cell != null) {
	    				    cellIndex = cell.cellIndex;	
	    				}	    				
	    				cell = rows.insertCell(cellIndex + 1);	  
	    				this._initCellEvent(cell);
	    				this._setCellStyle(cell);	    			    
	    				cell = null;
	    			}
	    			rows = null;
	    		}
	    	} else {
	    		//
	    		//删除掉不需要把单元格
	    		for(var i = this._selRect.erow; i >= this._selRect.brow; i--) {
	    			for(var j = this._selRect.ecol; j >= this._selRect.bcol; j--) {
	    				var cell = this._getCellObj(i, j);
	    				if(cell != null) {
	    					if(i > this._selRect.brow || j > this._selRect.bcol) {
	    					    this._table.rows[i].deleteCell(cell.cellIndex);
	    					} else {
	    						//合并单元格
	    						cell.colSpan = colSpan;
	    						cell.rowSpan = rowSpan;
	    					}
	    				}
	    			}
	    		}
	    	}
	    	ccell = null;
	    	this._refreshSheet();
	    },
	    /**
	     * 获得对应单元格对象.
	     */
	    _getCellObj:function(rindex, cindex) {
	    	var row = this._table.rows[rindex];
	    	var cell = null;
	    	for(var j = 1, len = row.cells.length; j < len; j++) {
	    		var tindex = this._getCellIndex(row.cells[j]);
	    		if(tindex == cindex) {
	    			cell = row.cells[j];
	    			break;
	    		}
	    	}
	    	return cell;
	    },	    
	    _doBlur:function() {
	    	if(this._selected == false) return;
	    	this.base();
		    //this._table.style.backgroundColor = "";
		    this.refresh();		    
	    },
	    doFocus:function() {
	    	if(this._selected && !this._cellMouseDown) return;
	    	this.base();	
	    	this.refresh();
	    	//this._table.style.backgroundColor = "#D6F8DA";
	    },
	    /**
	     * 刷新sheet对象列表
	     */
	    _refreshSheet:function() {
	    	this._sheet = [];
	    	for(var i = 1; i <= this.getOption().rows; i++) {
	    		this._sheet[i] = [];
	    		for(var j = 1; j <= this.getOption().cols; j++) {
	    			this._sheet[i][j] = null;
	    		}
	    	}
	    	//对应生成 sheet
	    	for(var i = 1; i <= this.getOption().rows; i++) {
	    	    var row = this._table.rows[i];
	    	    var k = 1;
	    	    var rs = this._sheet[i];
	    	    for(var j = 1, len = row.cells.length; j < len; j++) {
	    	    	while(rs[k] != null) {
	    	    		k++;
	    	    	}
	    	    	var ccell = this._table.rows[i].cells[j];
	    	    	var rowSpan = this._getNumberValue(ccell.rowSpan, 1);
	    	    	var colSpan = this._getNumberValue(ccell.colSpan, 1);
	    	    	if(rowSpan > 1 || colSpan > 1) {
		    	    	for(var ki = i; ki < i + rowSpan; ki++) {
		    	    		for(var kj = k; kj < k + colSpan; kj++) {
		    	    			this._sheet[ki][kj] = ccell;
		    	    		}	    	    		
		    	    	}
	    	    	} else {
	    	    		rs[k] = ccell;
	    	    	}	 
	    	    	ccell = null;
	    	    }
	    	    rs = null;
	    	    row = null;
	    	}    
	    },
	    _refreshTdStyle:function() {
	    	//设置选择背景
	    	for(var i = 1; i <= this.getOption().rows; i++) {
	    		var row = this._table.rows[i];
	    		for(var j = 1, len = row.cells.length; j < len; j++) {
	    			var hasSel = false;
	    			var cell = row.cells[j];
	    			var cindex = this._getCellIndex(cell);
	    			if(this._selRect.brow <= i && this._selRect.erow >= i 
	    					&& this._selRect.bcol <= cindex && this._selRect.ecol >= cindex) {
	    				hasSel = true;
	    			}
	    			if(hasSel && this._selected) {
    					this.setStyle(cell, "print_table_sel");
	    			} else if(row.dataFields != null && row.dataFields !== "") {
	    				this.setStyle(cell, "print_table_row_circle");	    				
    				} else {
    					this.setStyle(cell, "print_table_cell");
    				}
	    			cell.style.borderStyle = this.getOption().borderStyle;
	    			if(cell._option == null) {
    					cell._option = {};
    				}
	    			if(this.getOption().scale == 1) {	    				
	    				cell._option.fontSize = this._getNumberValue(cell.style.fontSize,12);
	    			}
	    			//字体缩小处理
	    			if(this.getOption().scale != 1 && cell._option.fontSize != null) {
	    				cell.style.fontSize = parseInt(cell._option.fontSize * this.getOption().scale, 10) + "px";
	    			}
	    			cell = null;
	    		}
	    	}
	    },
	    _refreshTdWidth:function() {
	    	if(this.isPecent(this.getOption().width)) {
	    		//
	    	} else {
		    	this.getOption().width = parseInt(this.getOption().width, 10);
		    	if(this._oldWidth != null && this._oldWidth != parseInt(this.getOption().width * this.getOption().scale, 10)) {
		    		var changedWidth = parseInt(this.getOption().width * this.getOption().scale, 10) - this._oldWidth;
		    		var changedAvgWidth = parseInt(changedWidth/this.getOption().cols, 10);
		    		var sumw = 0;
		    		for(var i = 1; i < this.getOption().cols; i++) {
		    			var ow = this._getNumberValue(this._table.rows[0].cells[i].style.width, 1);
		    			this._setNumberValue(this._table.rows[0].cells[i], "width", ow + changedAvgWidth);
		    			sumw += changedAvgWidth;
		    		}
		    		var ow = this._getNumberValue(this._table.rows[0].cells[this.getOption().cols].style.width, 1);
	    			this._setNumberValue(this._table.rows[0].cells[this.getOption().cols], "width", ow + changedWidth - sumw);
		    	}
		    	this._oldWidth = parseInt(this.getOption().width * this.getOption().scale, 10);
	    	}
	    },
	    _refreshTdHeight:function() {
	    	this.getOption().height = parseInt(this.getOption().height, 10);
	    	if(this._oldHeight != null && this._oldHeight != parseInt(this.getOption().height * this.getOption().scale , 10)) {
	    		var changedHeight = parseInt(this.getOption().height * this.getOption().scale, 10) - this._oldHeight;
	    		var changedAvgHeight = parseInt(changedHeight/this.getOption().rows, 10);
	    		var sumh = 0;
	    		for(var i = 1; i < this.getOption().rows; i++) {
	    			var oh = this._getNumberValue(this._table.rows[i].cells[0].style.height, 1);
	    			this._setNumberValue(this._table.rows[i].cells[0], "height", oh + changedAvgHeight);
	    			sumh += changedAvgHeight;
	    		}
	    		var oh = this._getNumberValue(this._table.rows[this.getOption().rows].cells[0].style.height, 1);
    			this._setNumberValue(this._table.rows[this.getOption().rows].cells[0], "height", oh + changedHeight - sumh);
	    	}
	    	this._oldHeight = parseInt(this.getOption().height * this.getOption().scale, 10);	
	    },
	    refresh:function() {
	    	this.base();
	    	//刷新td宽度
	    	this._refreshTdWidth();
	    	//刷新td高度
	    	this._refreshTdHeight();
	    	//刷新td样式
	    	this._refreshTdStyle();
	    	
	    	if(this._selectType == "td" && !this._cellMouseDown && this._selected) {	    	    	
		    		for(var i = this._selRect.brow; i <= this._selRect.erow; i++) {
		    			for(var j = this._selRect.bcol; j <= this._selRect.ecol; j++) {
		    				var cell = this._getCellObj(i, j);
		    				if(cell != null) {		    		
		    					var fontSize = this.getOption().fontSize || 12;
		    					var csp = this._getNumberValue(cell.colSpan, 1);
		    					var rsp = this._getNumberValue(cell.rowSpan, 1);
		    					cell._option.fontSize = fontSize;
		    					fontSize = parseInt(fontSize * this.getOption().scale, 10);
		    					if(this._selRect.erow - this._selRect.brow == rsp - 1 
		    							&& this._selRect.ecol - this._selRect.bcol == csp - 1) {
			    					if(cell.childComponent == null || this.isArray(cell.childComponent) && cell.childComponent.length == 0) {
			    						this._setHtml(cell, this.getOption().text || "", false);
			    					}
			    					cell.dataField = this.getOption().dataField;
			    					if(cell.parentNode.dataFields != this.getOption().dataFields) {
			    					    cell.parentNode.dataFields = this.getOption().dataFields;
			    					}
		    					}
		    					//行高
		    					if(this._selRect.brow == this._selRect.erow && this.getOption().rowHeight != null && this.getOption().rowHeight !== "") {
		    						var hcell = this._table.rows[i].cells[0];
		    						var oldHeight = this._getNumberValue(hcell.style.height);
		    						var newHeight = parseInt(parseInt(this.getOption().rowHeight, 10) * this.getOption().scale, 10);
		    						var ch = newHeight - oldHeight;
		    						this._setNumberValue(hcell, "height", newHeight);
		    						this._oldHeight = this.getOption().height + ch;
		    						this.setHeight(this._oldHeight);
		    						hcell = null;
		    					}
		    					cell.style.textAlign = this.getOption().textAlign || "";
			    				cell.style.fontSize = fontSize + "px";		    					
			    				cell.style.fontWeight = this.getOption().fontWeight || "normal";
			    				cell.style.fontFamily = this.getOption().fontFamily || this.getI18n("qzz.defFontFamily", "宋体");
			    				cell.style.fontStyle = this.getOption().fontStyle || "normal";
			    				cell.style.textDecoration = this.getOption().textDecoration || "none";
			    				cell.style.color = this.getOption().color || "";
			    				cell.style.backgroundColor = this.getOption().backgroundColor || "";		    					
		    				}
		    			}
		    		}
	    	}
	    	if(this.focus) {
	    		this.thisWindow.style.overflow = "visible";	    		
	    	} else {
	    		this.thisWindow.style.overflow = "hidden";
	    	}
	    },
	    /**
	     * 初始化事件.
	     */
	    _initCellEvent:function(col) {
	    	var _this = this;
	    	col.onmouseover = function(ev) {
				var ev = ev || window.event;
				_this.doCellMouseOver(this, ev);
			}
	    	col.onmousemove = function(ev) {
				var ev = ev || window.event;
				_this.doCellMouseMove(this, ev);
			}
	    	col.onmousedown = function(ev) {
				var ev = ev || window.event;
				_this.doCellMouseDown(this, ev);
			}
	    	col.onmouseup = function(ev) {
				var ev = ev || window.event;
				_this.doCellMouseUp(this, ev);
			}
	    },
	    /**
	     * 获得单元格在table中的真实例序号
	     */
	    _getCellIndex:function(cell) {
	    	if(cell == null) return -1;
	    	var rowIndex = cell.parentNode.rowIndex;
	    	if(rowIndex == 0) {
	    		return cell.cellIndex;
	    	}
	    	var colIndex = 1;
	    	for(var j = 1; j <= this.getOption().cols; j++) {
	    		if(this._sheet[rowIndex][j] === cell) {
	    			colIndex = j;
	    			break;
	    		}
	    	}	
	    	return colIndex;
	    },
	    /**
	     * 获得顶cell.
	     */
	    _getTopCell:function(cell) {
	    	var cindex = cell;
	    	if(typeof cindex === "object") {
	    		cindex = this._getCellIndex(cell);
	    	}
	    	return this._table.rows[0].cells[cindex];
	    },
	    /**
	     * 执行标题鼠标按下事件.
	     * @param cell 控件对象
	     * @param ev 事件对象
	     */
	    doCellMouseDown:function(cell, ev) {
	    	if(this.getMouseButton(ev) == this.mouseType.mtLeft) {
	    		if(this.hasbind("onBeforeSelCell")) {
	    			this.trigger("onBeforeSelCell",[]);
	    		}	    		
		    	this._cellMouseDown = true;
		    	this._selected = true;
		        this.curCell = cell;
		        this.curCellWidth = this._getColWidth(cell);
		        var cindex = this._getCellIndex(cell);
		        if(cindex > 0 && cindex < this.getOption().cols) {
		            this.nextCellWidth = this._getColWidth(this._table.rows[0].cells[cindex + 1], 1);
		        }
		        this.curTableWidth = this.getOption().width;
		        this.curMouseLeft = this.mouseLeft;
		        var colSpan = this._getNumberValue(cell.colSpan, 1);
		        var rowSpan = this._getNumberValue(cell.rowSpan, 1);
		        this._selRect = {"brow":cell.parentNode.rowIndex,
		        		         "erow":cell.parentNode.rowIndex + rowSpan - 1,
		        		         "bcol":cindex,
		        		         "ecol":cindex + colSpan - 1
		        		         };
//		    	var cell = this._getCellObj(this._selRect.brow, this._selRect.bcol);
//		    	if(cell != null && this.isArray(cell.childComponent) && cell.childComponent.length > 0) {
//					cell.childComponent[0].setFocus();
//		    	}
		        if(cell != null && this.isArray(cell.childComponent) && cell.childComponent.length > 0) {
					cell.childComponent[0].setFocus();
					this._cellMouseDown = false;
		    	} else {
		        if(this.focus != true) {
	    			this.setFocus();
	    		}
		    	}
	        }
	    },
	    /**
	     * 获得原来的宽度
	     */
	    _getColWidth:function(cell) {
	    	return this._getNumberValue(this._table.rows[0].cells[this._getCellIndex(cell)].style.width, 1);
	    },
	    
	    /**
	     * 调整当前列宽度
	     */
	    _setColWidth:function(cell, addWidth) {
	    	var border = this._getNumberValue(this.getOption().border, 1);
	    	var cindex = this._getCellIndex(cell);
	    	var colSpan = this._getNumberValue(cell.colSpan, 1);
	    	cindex = cindex + colSpan - 1;
	    	if(cindex > 0 && cindex < this.getOption().cols) { 
	    		if(this.nextCellWidth - addWidth >= border && this.curCellWidth + addWidth >= border) {
			    	this._setNumberValue(this._table.rows[0].cells[cindex], "width", this.curCellWidth + addWidth);
			    	this._setNumberValue(this._table.rows[0].cells[cindex + 1], "width", this.nextCellWidth - addWidth);
	    		}
	    	} else if(cindex == this.getOption().cols) {
	    		this._setNumberValue(this._table.rows[0].cells[cindex], "width", this.curCellWidth + addWidth);
	    		this.getOption().width = this.curTableWidth + parseInt(addWidth / this.getOption().scale, 10);
	    		this._oldWidth = parseInt(this.getOption().width  * this.getOption().scale, 10);
	    		this._setNumberValue(this.thisWindow, "width", this._oldWidth);
	    		if(this.hasbind("onFocus")) {
		    		this.trigger("onFocus",[]);
		    	}
	    	}
	    },
        /**
         * 执行标题鼠标弹起事件.
	     * @param cell 控件对象
	     * @param ev 事件对象
         */	    
	    doCellMouseUp:function(cell, ev) {
	    	if(this.getMouseButton(ev) == this.mouseType.mtLeft && !this.resizeState) {
	    		this.refresh()
	    		if(this.hasbind("onFocus") && this._selectType == "td") {
			    		var cell = this._getCellObj(this._selRect.brow, this._selRect.bcol);
			    		if(cell != null) {
			    			this.getOption().text = cell.innerHTML;
			    			this.getOption().textAlign = cell.style.textAlign || "left";
				    		this.getOption().fontWeight = cell.style.fontWeight || "normal";
				    		this.getOption().fontStyle = cell.style.fontStyle || "normal";
				    		this.getOption().fontFamily = cell.style.fontFamily || this.getI18n("qzz.defFontFamily", "宋体");
				    		this.getOption().textDecoration = cell.style.textDecoration || "none";
				    		var color = cell.style.color || "";
				    		if(color !== "") {
				    		    this.getOption().color = this.rgbToColor(color);
				    		} else {
				    			this.getOption().color = "";
				    		}
				    		var bgcolor = cell.style.backgroundColor || "";
				    		if(bgcolor !== "") {
				    		    this.getOption().backgroundColor = this.rgbToColor(bgcolor);
				    		} else {
				    			this.getOption().backgroundColor = "";
				    		}
			    			this.getOption().dataField = cell.dataField || "";
			    			this.getOption().dataFields = cell.parentNode.dataFields || "";
			    			if(this._selRect.brow == this._selRect.erow) {
			    				var hcell = this._table.rows[this._selRect.brow].cells[0];
			    				var ch = this._getNumberValue(hcell.style.height, 24);
			    			    this.getOption().rowHeight = parseInt(ch/this.getOption().scale, 10);
			    			    hcell = null;
			    			} else {
			    				this.getOption().rowHeight = "";
			    			}
			    			this.getOption().fontSize = cell._option.fontSize; //parseInt(this._getNumberValue(cell.style.fontSize, 12)/this.getOption().scale, 10);
			    			cell = null;
			    		    this.trigger("onFocus",[true]);
			    		}
			    }		    	
	    	}
	    },
	    /**
	     * 执行标题鼠标移动事件.
	     * @param cell 控件对象
	     * @param ev 事件对象
	     */
	    doCellMouseMove:function(cell, ev) {
	    	if(!this._cellMouseDown) {   
	    		var sleft = 0;
		        var oRect = cell.getBoundingClientRect();
			    var disX = ev.clientX + sleft;
		        var w = cell.offsetWidth;
		        var cLeft = ev.clientX - oRect.left;
		        var vr = w - cLeft;
		        //this.logInfo(cLeft);
		        if (vr < 5) {
		        	this.resizeState = true;
		        	this.titleDragState = true;
		        	if(this.getBrowser("safari")) {
		        		cell.style.cursor = "e-resize";
		        	} else {
		        		cell.style.cursor = "col-resize";		        		
		        	}
		        } else if(cell.cellIndex == 0 && cLeft < 5) {
		        	cell.style.cursor = "move";
		        	this.titleDragState = false;
		        	this.resizeState = false;
		        } else {
		        	this.titleDragState = false;
		        	this.resizeState = false;
		        	cell.style.cursor = "";
		        }
		        this.mouseLeft = ev.clientX;
		        sleft = null;
		        oRect = null;
		        disX = null;
		        w = null;
		        vr = null;
	        }
	    },
	    /**
	     * 执行标题鼠标移过事件.
	     * @param cell 控件对象
	     * @param ev 事件对象
	     */
	    doCellMouseOver:function(cell, ev) {
	    	if(this._cellMouseDown && this.getMouseButton(ev) == this.mouseType.mtLeft && !this.resizeState) {
	    		//this.logInfo(cell.cellIndex);
		    	//this._selRect.push(cell);
	    		var cindex = this._getCellIndex(cell);
	    		var colSpan = this._getNumberValue(cell.colSpan, 1);
		        var rowSpan = this._getNumberValue(cell.rowSpan, 1);
		        var hasMerg = false;
	    		if(cindex + colSpan - 1 > this._selRect.ecol) {
	    			this._selRect.ecol = cindex + colSpan - 1;
	    			hasMerg = true;
	    		} else if(cindex < this._selRect.bcol) {
	    			this._selRect.bcol = cindex;
	    			hasMerg = true;
	    		}
	    		var rindex = cell.parentNode.rowIndex;
	    		if(rindex + rowSpan - 1 > this._selRect.erow) {
	    			this._selRect.erow = rindex + rowSpan - 1;
	    			hasMerg = true;
	    		} else if(rindex < this._selRect.brow) {
	    			this._selRect.brow = rindex;
	    			hasMerg = true;
	    		}
	    		if(hasMerg) {
		    		for(var i = this._selRect.brow; i <= this._selRect.erow; i++) {
		    			var rs = this._sheet[i];
		    			for(var j = this._selRect.bcol; j <= this._selRect.ecol; j++) {
		    				var cc = rs[j];
		    				colSpan = this._getNumberValue(cc.colSpan, 1);
		    		        rowSpan = this._getNumberValue(cc.rowSpan, 1);
		    		        if(colSpan > 1 || rowSpan > 1) {
		    		        	this.doCellMouseOver(cc, ev);
		    		        }
		    			}
		    		}
	    		}
	    		
	    	}
	    },
	    doMouseUp:function() {
	    	this._cellMouseDown = false;
	    },
	    doMouseMove:function(ev) {
	    	if(this._cellMouseDown && this.resizeState && ev.clientX != this.curMouseLeft) {
	    		this._setColWidth(this.curCell, ev.clientX - this.curMouseLeft);
	    	}
	    },
	    /**
	     * 控制选择事件函数.
	     */
	    _onselectstart:function(e) {
	    	var ent = e || window.event;
            var tg = ent.target || ent.srcElement;
            var cansel = false;
//            if(!this.shiftKeyDown) {	            
//	            var cansel = tg.tagName == 'INPUT' || tg.tagName == "TEXTAREA";		            
//            }	
            ent.returnValue = cansel;
            return cansel;
	    },
	    getAttribute:function() {
	    	var attr = this.base();
	    	attr.push({"name":"cols","value":this.getOption().cols});
	    	attr.push({"name":"rows","value":this.getOption().rows});
	    	attr.push({"name":"heads","value":this.getOption().heads});
	    	attr.push({"name":"borderStyle","value":this.getOption().borderStyle});
	    	if(this._selectType == "td") {	
	    		attr.push({"name":"rowHeight","value":this.getOption().rowHeight});
	    		attr.push({"name":"dataFields","value":this.getOption().dataFields});
	    		var cell = this._sheet[this._selRect.brow][this._selRect.bcol];
	    		if(cell != null && this.isArray(cell.childComponent) && cell.childComponent.length > 0) {
	    			//
	    		} else {	    	        
	    	        attr.push({"name":"dataField","value":this.getOption().dataField});
	    	        attr.push({"name":"text","value":this.getOption().text});
	    		}	    
	    		attr.push({"name":"textAlign","value":this.getOption().textAlign || "left"});
		    	attr.push({"name":"fontFamily","value":this.getOption().fontFamily});
		    	attr.push({"name":"fontSize","value":this._getNumberValue(this.getOption().fontSize, 12)});	
			    attr.push({"name":"fontWeight","value":this.getOption().fontWeight || "normal"});
			    attr.push({"name":"fontStyle","value":this.getOption().fontStyle || "normal"});
			    attr.push({"name":"textDecoration","value":this.getOption().textDecoration || "none"});
			    attr.push({"name":"color","value":this.getOption().color || ""});
			    attr.push({"name":"backgroundColor","value":this.getOption().backgroundColor || ""});
	    	}
	    	return attr;
	    },
	    setOption:function(key, value, oldValue) {
	    	if(value != oldValue) {	
	    		this.getOption()[key] = value;		    	
	    		if(key == "cols") {
	    			this._changeCol();
	    		} else if(key == "rows") {
	    			this._changeRow();
	    		}	   
	    		this.trigger("onAttribChanged",[this, key, oldValue, value]);
	    	}
	    },
	    setColWidths:function(colWidths) {
	    	if(colWidths == null) return;
	    	if(typeof colWidths == "string") {
	    		colWidths = colWidths.split(",");
	    	}
	    	var len = colWidths.length;
	    	var tr = this._table.rows[0];
	    	for(var j = 0; j < len; j++) {
	    		tr.cells[j + 1].style.width = parseInt(parseInt(colWidths[j], 10) * this.getOption().scale, 10) + "px";
	    	}
	    },
	    getColWidths:function() {
	    	var cws = [];
	    	var tr = this._table.rows[0];
	    	var len = tr.cells.length - 1;
	    	for(var j = 0; j < len; j++) {
	    		var w = this._getNumberValue(tr.cells[j + 1].style.width);
	    		w = parseInt(w / this.getOption().scale, 10);
	    		cws.push(w);
	    	}
	    	tr = null;
	    	return cws;
	    },
	    setAttribute:function(AttrAry) {
	    	this._oldWidth = parseInt(this.getOption().width * this.getOption().scale, 10);
	    	this._oldHeight = parseInt(this.getOption().height * this.getOption().scale, 10);
	    	this.base(AttrAry);
	    },
	    getRow:function(r) {
	    	return this._table.rows[r];
	    },
	    /**
	     * 返回单元格
	     */
	    getCell:function(row, c) {
	    	var cells = null;
	    	if(row.cells != null) {
	    		cells = row.cells;
	    	} else if(typeof row == "number"){
	    		cells = this._table.rows[row].cells;
	    	}
	    	if(cells.length > c) {
	    		return cells[c];
	    	} else {
	    		return null;
	    	}
	    }
	});
	
	/**
	 * 表格控件.
	 */
	_bindRenderFunc("qzztable", qzz.ui.design.table);
	return qzz.ui.design.table;
});